
class Liczba
{
	private int n;
	private String[] regexes = {"[0-1]+$",
								"[0-2]+$",
								"[0-3]+$",
								"[0-4]+$",
								"[0-5]+$",
								"[0-6]+$",
								"[0-7]+$",
								"[0-8]+$",
								"[0-9]+$",
								"[0-9aA]+$",
								"[0-9a-bA-B]+$",
								"[0-9a-cA-C]+$",
								"[0-9a-dA-D]+$",
								"[0-9a-eA-E]+$",
								"[0-9a-fA-F]+$",
								"[0-9a-gA-G]+$"};

	Liczba()
	{
		this.n = 0;
	}

	Liczba(String n, int podstawa) throws LiczbaException
	{
		if (podstawa < 2 || podstawa > 17)
		{
			throw new LiczbaException(n + " - niewlasciwa podstawa (podano " + podstawa + ")");
		}

		if ( ! n.matches(this.regexes[podstawa - 2]))
		{
			throw new LiczbaException(n + " ma zly format");
		}

		if (podstawa == 10)
		{
			try
			{
				this.n = Integer.parseInt(n);
			}
			catch (NumberFormatException e)
			{
				throw new LiczbaException(e.getMessage());
			}
		}
		else
		{
			int val = 0;
			int i = 0;
			int pos;
			int multiplier;
			while (i < n.length())
			{
				pos = n.length() - i - 1;
				multiplier = (int)Math.pow(podstawa, pos);
				switch (n.charAt(i))
				{
					case '0':
						val += 0 * multiplier;
						break;
					case '1':
						val += 1 * multiplier;
						break;
					case '2':
						val += 2 * multiplier;
						break;
					case '3':
						val += 3 * multiplier;
						break;
					case '4':
						val += 4 * multiplier;
						break;
					case '5':
						val += 5 * multiplier;
						break;
					case '6':
						val += 6 * multiplier;
						break;
					case '7':
						val += 7 * multiplier;
						break;
					case '8':
						val += 8 * multiplier;
						break;
					case '9':
						val += 9 * multiplier;
						break;
					case 'A':
						val += 10 * multiplier;
						break;
					case 'B':
						val += 11 * multiplier;
						break;
					case 'C':
						val += 12 * multiplier;
						break;
					case 'D':
						val += 13 * multiplier;
						break;
					case 'E':
						val += 14 * multiplier;
						break;
					case 'F':
						val += 15 *  multiplier;
						break;
					case 'G':
						val += 16 *  multiplier;
						break;
					default:
						throw new LiczbaException("Podana liczba ma zly format");
				}
				++i;
			}
			this.n = val;
		}
	}

	public String zapis(int podstawa) throws LiczbaException
	{
		if (podstawa < 2 || podstawa > 17)
		{
			throw new LiczbaException("Niewlasciwa podstawa. Podano "+podstawa);
		}

		int decimal = this.n;
		int power = 1;
		int digits = 1;
		do
		{
			power *= podstawa;
			++digits;
		}
		while (power < decimal);

		String output = "";
		int currentDigit = 0;
		while (digits > 0)
		{
				currentDigit = (int)Math.floor(decimal / power);
				decimal -= currentDigit * power;
				
				switch (currentDigit)
				{
					case 0:
						if (output.length() > 0)
						{
							output += "0";
						}
						break;
					case 1:
						output += "1";
						break;
					case 2:
						output += "2";
						break;
					case 3:
						output += "3";
						break;
					case 4:
						output += "4";
						break;
					case 5:
						output += "5";
						break;
					case 6:
						output += "6";
						break;
					case 7:
						output += "7";
						break;
					case 8:
						output += "8";
						break;
					case 9:
						output += "9";
						break;
					case 10:
						output += "A";
						break;
					case 11:
						output += "B";
						break;
					case 12:
						output += "C";
						break;
					case 13:
						output += "D";
						break;
					case 14:
						output += "E";
						break;
					case 15:
						output += "F";
						break;
					case 16:
						output += "G";
						break;
				}
				power /= podstawa;
				--digits;
		}

		return output.length() < 1 ? "0" : output;
	}

}